package ru.spbu.math.android.cbrf.view;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.model.RateType;


/**
 * Created by IntelliJ IDEA. User: yan lobkarev mail: yan.lobkarev@gmail.com Date: 30.04.2010 Time: 12:42:48
 */
public class OnRateTypeClickListener implements AdapterView.OnItemSelectedListener {

    private String baseTitleForSpinner;
    private static LinearLayout lastSelectedView;
    private static int allListenersInOneActivity = 0;
    private static int allListenersHaveBeenShowedInOneActivity = 0;
    private static boolean isActivated = false;
    private static RateType lastSelectedRateType;
    private static String LOG_TAG = "OnRateTypeClickListener";

    public static RateType getLastSelectedRateType() {
        return lastSelectedRateType;
    }

    public static void deactivate() {
        allListenersHaveBeenShowedInOneActivity = 0;
        allListenersInOneActivity = 0;
        lastSelectedRateType = null;
        lastSelectedView = null;
        isActivated = false;
    }

    public OnRateTypeClickListener(String ratesTypeGroup) {
        super();
        allListenersInOneActivity++;
        baseTitleForSpinner = ratesTypeGroup;
    }

    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d(LOG_TAG, view.getClass() + "; selected: " + i + "; item: " + adapterView.getItemAtPosition(i));
        if (isActivated) {
            if (lastSelectedView != null) {
                ImageView iv = (ImageView) lastSelectedView.findViewById(R.id.item_icon);
                iv.setImageResource(R.drawable.list_item_icon_inactive);
                TextView optionalView = (TextView) lastSelectedView.findViewById(R.id.bottomtext);
                optionalView.setText("");
            }
            lastSelectedView = (LinearLayout) view;
            TextView tv = (TextView) lastSelectedView.findViewById(R.id.toptext),
                    ov = (TextView) lastSelectedView.findViewById(R.id.bottomtext);
            ImageView iv = (ImageView) lastSelectedView.findViewById(R.id.item_icon);
            lastSelectedRateType = (RateType) adapterView.getItemAtPosition(i);
            ov.setText(tv.getText());
            tv.setText(baseTitleForSpinner);
            iv.setImageResource(R.drawable.list_item_icon_active);

        } else {

            allListenersHaveBeenShowedInOneActivity++;
            if (allListenersHaveBeenShowedInOneActivity == allListenersInOneActivity) {
                isActivated = true;
            }
            setViewAsUnselected(view);

        }
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
        //  do nothing
    }

    private void setViewAsUnselected(View view) {
        ImageView iv = (ImageView) view.findViewById(R.id.item_icon);
        iv.setImageResource(R.drawable.list_item_icon_inactive);
        TextView optionalView = (TextView) view.findViewById(R.id.bottomtext),
                baseView = (TextView) view.findViewById(R.id.toptext);
        optionalView.setText("");
        baseView.setText(baseTitleForSpinner);
    }

}
